package example.tallydemo.MyFragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CalendarView;
import android.widget.ListView;

import example.tallydemo.MyActivity.ShowDetailActivity;
import example.tallydemo.MyAdapter.MyListAdapter;
import example.tallydemo.MyContextMeau.TzhContextMenu;
import example.tallydemo.MyEntity.AccountBook;
import example.tallydemo.MyEntity.AccountManager;
import example.tallydemo.MyTools.MyTimeTool;
import example.tallydemo.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class HistoryFragment extends MyBaseFragment
{

    CalendarView calendarView;
    AccountManager accountManager = null;
    MyListAdapter myListAdapter;
    ListView listView;
    MyTimeTool myTimeTool;

    View view;
    int year;
    int month;
    int day;

    public void setFragment(AccountManager accountManager)
    {
        this.accountManager = accountManager;
    }

    public HistoryFragment()
    {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment

        view = inflater.inflate(R.layout.fragment_history, container, false);

        onCreate(view);
        return view;
    }

    public void onCreate(View view)
    {

        myTimeTool = new MyTimeTool();
        myTimeTool.setTody();
        year = myTimeTool.getYear();
        month = myTimeTool.getMonth();
        day = myTimeTool.getDay();
        calendarView = view.findViewById(R.id.id_calendarView);
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener()
        {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int year, int month, int dayOfMonth)
            {
                setDate(year,month,dayOfMonth);
                update();
            }
        });


        myListAdapter = new MyListAdapter(getContext());
        listView = view.findViewById(R.id.id_history_listview);
        listView.setAdapter(myListAdapter);

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
            {
                AccountBook it = (AccountBook) myListAdapter.getItem(position);
                onItemLongClicked(view, it);
                return false;
            }
        });


        myListAdapter.addBooks(accountManager.queryDate(year, month, day));

    }

    private void onItemLongClicked(final View view, final AccountBook book)
    {
        TzhContextMenu contextMenu = new TzhContextMenu();
        contextMenu.addContextItem("删除", "remove");
        contextMenu.addContextItem("详细", "detail");

        contextMenu.show(getContext(), view, view.getWidth() / 2, -view.getHeight() / 2);//显示位置
        contextMenu.listener = new TzhContextMenu.OnContextMenuItemClickedListener()
        {
            @Override
            public void onContextMenuItemClicked(String option, String value)
            {
                if (value.equals("remove"))
                {
                    onDelItem(view, book);
                } else if (value.equals("detail"))
                {
                    onDetailItem(view, book);
                }
            }
        };
    }

    public void onDelItem(View view1, AccountBook it)
    {
        accountManager.delete(it);
        myListAdapter.delAll();
        myListAdapter.addBooks(accountManager.queryDate(year, month, day));
        myListAdapter.notifyDataSetChanged();
    }

    public void onDetailItem(View view, AccountBook it)
    {
        Intent intent = new Intent(getContext(), ShowDetailActivity.class);

        if (it.getType())
        {
            intent.putExtra("type", "收入");
        } else
        {
            intent.putExtra("type", "支出");
        }
        intent.putExtra("id", it.getId());
        intent.putExtra("origin", it.getOrigin());
        intent.putExtra("classify", it.getClassify());
        intent.putExtra("money", String.valueOf(it.getMoney()));
        intent.putExtra("date", myTimeTool.DateToString(it.getDate()));
        intent.putExtra("introduction", it.getIntroduction());
        startActivity(intent);
    }


    public void setDate(int year, int month, int day)
    {
        this.year = year;
        this.month = month + 1;
        this.day = day;
    }


    public void update()
    {
        if (myListAdapter != null)
        {
            myListAdapter.delAll();
            myListAdapter.addBooks(accountManager.queryDate(year, month, day));
            myListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        update();
    }


    @Override
    public void setUserVisibleHint(boolean hidden)
    {
        // TODO Auto-generated method stub
        super.setUserVisibleHint(hidden);
        if (hidden)
        {
            update();
        }
    }
}